package cn.anlucky.apiadmin.controller;


import cn.anlucky.apiadmin.pojo.Userinfo;
import cn.anlucky.apiadmin.service.LoginService;
import cn.anlucky.apiadmin.service.UserRoleService;
import cn.anlucky.apiadmin.vo.LoginUserInfoVo;
import cn.anlucky.apiadmin.vo.R;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class LoginController {


    @Autowired
    private LoginService loginService;

    @Autowired
    private UserRoleService userRoleService;

    /**
     * 用户登录
     * @param userinfo
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody Userinfo userinfo){
        R login = loginService.login(userinfo);
        return login;
    }


    /**
     * 取用户角色列表
     * @return
     */
    @PostMapping("/getUserRoleInfo")
    public R getUserRoleInfo(){
        List<String> roleList = StpUtil.getRoleList();
        LoginUserInfoVo loginUserInfoVo = new LoginUserInfoVo();
        Object userName = StpUtil.getExtra("userName");
        loginUserInfoVo.setUserName(userName.toString());
        loginUserInfoVo.setRoleList(roleList);
        return R.success(loginUserInfoVo);
    }

    /**
     * 退出登录
     * @return
     */
    @PostMapping("/logOut")
    public R logOut(){
        Object loginId = StpUtil.getLoginId();
        StpUtil.logout(loginId);
        return R.success("退出登录成功");
    }




}
